package com.sqx.modules.goodsType.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.dao.GoodsDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goodsType.dao.GoodsTypeDao;
import com.sqx.modules.goodsType.entity.GoodsType;
import com.sqx.modules.goodsType.service.GoodsTypeService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品分类-管理端
 *
 * @author wuChang
 * @since 2024-05-30
 */
@RestController
@Api(value = "商品分类-管理端", tags = {"商品分类-管理端"})
@RequestMapping("/admin/goodsType/")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsTypeDao goodsTypeDao;

    /**
     * 新增或修改商品分类
     *
     * @param goodsType 商品分类
     */
    @ApiOperation("新增或修改商品分类")
    @PostMapping(value = "saveGoodsType")
    public Result saveGoodsType(@ApiParam("商品分类实体") GoodsType goodsType) {
        return goodsTypeService.saveGoodsType(goodsType);
    }

    /**
     * 获取商品分类列表
     *
     * @param goodsType 商品分类
     */
    @ApiOperation("获取商品分类列表")
    @GetMapping(value = "getGoodsTypeList")
    public Result getGoodsTypeList(@ApiParam("页码") Integer page,
                                   @ApiParam("条数") Integer limit,
                                   @ApiParam("商品分类实体") GoodsType goodsType) {

        return Result.success().put("data", goodsTypeService.getGoodsTypeList(page, limit, goodsType));
    }

    /**
     * 删除商品分类
     *
     * @param typeId 商品分类id
     */
    @ApiOperation("删除商品分类")
    @PostMapping(value = "deleteGoodsTypeById")
    public Result deleteGoodsTypeById(@ApiParam("商品分类id") Long typeId) {
        GoodsType goodsType = goodsTypeService.getById(typeId);
        if (goodsType != null && goodsType.getParentId() == 0) {
            List<Goods> goodsList = goodsDao.selectList(new QueryWrapper<Goods>().eq("type_id", goodsType.getTypeId()));
            if (!goodsList.isEmpty()) {
                return Result.error("该分类下有关联，请勿删除");
            }
            List<GoodsType> goodsTypeList = goodsTypeDao.selectList(new QueryWrapper<GoodsType>().eq("parent_id", goodsType.getTypeId()));
            if (!goodsTypeList.isEmpty()) {
                for (GoodsType type : goodsTypeList) {
                    List<Goods> goodsLists = goodsDao.selectList(new QueryWrapper<Goods>().eq("type_id", type.getTypeId()));
                    if (!goodsLists.isEmpty()) {
                        return Result.error("该分类下有关联，请勿删除");
                    }
                }
            }
        }
        if (goodsType != null && goodsType.getParentId() != 0) {
            List<Goods> goods = goodsDao.selectList(new QueryWrapper<Goods>().eq("type_id", typeId));
            if (!goods.isEmpty()) {
                return Result.error("该分类下有关联，请勿删除");
            }
        }
        return goodsTypeService.removeById(typeId) ? Result.success() : Result.error();
    }


    /**
     * 获取商品分类详情
     *
     * @param typeId 商品分类id
     */
    @ApiOperation("获取商品分类详情")
    @GetMapping(value = "getGoodsTypeById")
    public Result getGoodsTypeById(@ApiParam("商品分类id") Long typeId) {
        return Result.success().put("data", goodsTypeService.getById(typeId));
    }


    @ApiOperation("获取一级商品分类列表")
    @GetMapping(value = "getGoodsTypeListByParentId")
    public Result getGoodsTypeListByParentId(@ApiParam("页码") Integer page,
                                             @ApiParam("条数") Integer limit) {
        return Result.success().put("data", goodsTypeService.getGoodsTypeListByParentId(page, limit));
    }

    @ApiOperation("获取二级商品分类列表")
    @GetMapping(value = "getGoodsTypeListByChild")
    public Result getGoodsTypeListByChild(@ApiParam("页码") Integer page,
                                          @ApiParam("条数") Integer limit,
                                          @ApiParam("一级分类id") Long typeId) {
        return Result.success().put("data", goodsTypeService.getGoodsTypeListByChild(page, limit, typeId));
    }

}